package com.sell.sell.controller;

import com.sell.sell.entity.ProductCategory;
import com.sell.sell.entity.ProductInfo;
import com.sell.sell.service.ProductCategoryService;
import com.sell.sell.service.ProductInfoService;
import com.sell.sell.util.ResultVOUtil;
import com.sell.sell.vo.ProductInfoVo;
import com.sell.sell.vo.ProductVO;
import com.sell.sell.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO list(){
        //获取所有的上架的商品
        List<ProductInfo> productInfos=productInfoService.findAllUp();
        List<Integer> typeList=productInfos.stream()
                .map(e->e.getCategoryType())
                .collect(Collectors.toList());

        //获取分类
        List<ProductCategory> productCategories=productCategoryService.findByCategoryTypeIn(typeList);
        //组装需要的数据结构
        List<ProductVO> productVOList=new ArrayList<>();
        for (ProductCategory productCategory: productCategories){
            ProductVO productVO=new ProductVO();
            productVO.setName(productCategory.getCategoryName());
            productVO.setType(productCategory.getCategoryType());
            List<ProductInfoVo> productInfoVoList=new ArrayList<>();

            for (ProductInfo productInfo:productInfos){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVo productInfoVo=new ProductInfoVo();
                    productInfoVo.setName(productInfo.getProductName());
                    productInfoVo.setDescription(productInfo.getProductDescription());
                    productInfoVo.setIcon(productInfo.getProductIcon());
                    productInfoVo.setPrice(productInfo.getProductPrice());
                    productInfoVo.setId(productInfo.getProductId());
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVO.setFoods(productInfoVoList);
            productVOList.add(productVO);
        }
        return ResultVOUtil.success(productVOList);
    }



}
